import os
import sys
import hashlib
from pathlib import WindowsPath, Path
import json


def CalcFileSha256(filname):
    ''' calculate file sha256 '''
    with open(filname, "rb") as f:
        sha256obj = hashlib.sha256()
        sha256obj.update(f.read())
        hash_value = sha256obj.hexdigest()
        return hash_value


def CalcFileSize(filename):
    ''' calculate file size '''
    return os.stat(filename).st_size


def list_gdcm_package_files():
    file_list = []

    root = "E:/medical/project/dcm/gdcm-2.8.9-py36_vc14_0"
    dirs = ['Library', 'Lib']
    for dir in dirs:
        cur_dir = Path(root).joinpath(dir)
        files = sorted(cur_dir.rglob("*.*"))
        file_list.extend(files)
    print(file_list)

    with open("files", "w") as f:
        for file in file_list:
            file = Path(file)
            if not file.is_file():
                continue
            file = str(file)
            file = file.replace("\\", "/")
            write_name =file[len(root)+1:]
            f.writelines(write_name+"\n")

def create_files_json():
    root = "E:/medical/project/dcm/gdcm-2.8.9-py36_vc14_0"
    paths_dict = {}
    paths_dict['paths'] = []
    paths_dict['paths_version'] = 1
    with open("files", "r") as f:
        for file in f:
            file = file.strip()
            whlole_file = os.path.join(root, file)
            print(os.path.exists(whlole_file))
            file_dict = {}
            file_dict['_path'] = file
            file_dict['path_type'] = "hardlink"
            file_dict['sha256'] = CalcFileSha256(whlole_file)
            file_dict['size_in_bytes'] = CalcFileSize(whlole_file)
            paths_dict['paths'].append(file_dict)

    json_file = 'paths.json'
    with open(json_file, "w", encoding="utf-8") as outfile:
        outfile.write(json.dumps(paths_dict))




if __name__ == '__main__':
    create_files_json()